const app = getApp()

Page({
  data: {
    company: []
  },
  //监听页面初次载入完成
  onLoad: function () {
    this.getApi();
  },
  //获取
  getApi: function () {
    var parms = {
      signkey: 'index',
    }
    app.api().Get('api/v1/bestbao/card/index', parms,(result) => {
      if (result.code == 200) {
        this.setData({
          company: result.data,
        });
      }
    })
    app.api().Get('api/v1/bestbao/card/news',{signkey: 'news'}, (result) => {
      if (result.code == 200) {
        this.setData({
          news: result.data,
        });
      }
    })
  },
  //拨打电话
  callPhone: function (e) {
    app.callPhone(e);
  },
  //地图定位
  maps: function () {
    var company = this.data.company;
    var latitude = company.latitude, longitude = company.longitude
    if (app.util().isNull(latitude) || app.util().isNull(longitude)) {
      wx.getLocation({
        type: 'wgs84',
        success: (res) => {
          longitude = res.longitude, latitude = res.latitude;
          wx.openLocation({
            name: company.title,
            longitude: parseFloat(longitude),
            latitude: parseFloat(latitude),
            scale: 18,
            address: company.address
          });
          this.setData({
            latitude: latitude,
            longitude: longitude,
            markers: [{
              latitude: latitude,
              longitude: longitude
            }]
          })
        }
      })
    } else {
      wx.openLocation({
        name: company.name,
        longitude: parseFloat(longitude),
        latitude: parseFloat(latitude),
        scale: 18,
        address: company.address
      });
      this.setData({
        latitude: latitude,
        longitude: longitude,
        markers: [{
          latitude: latitude,
          longitude: longitude
        }]
      })
    }
  },
  /**
    * 用户点击右上角分享
    */
  onShareAppMessage: function () {
    return {
      title: this.data.company.title,
      path: '/pages/index?ucode=' + app.globalData.loginuser.ucode
    }
  },
  /**
    * 保存电话到通讯录
    */
  addPhoneContact: function () {
    wx.addPhoneContact({
      firstName: this.data.company.title,
      remark: this.data.company.title,
      hostNumber: this.data.company.phone,
      mobilePhoneNumber: this.data.company.phone,
      photoFilePath: this.data.company.logo
    })
  }
})